package net.dgg.zz.credential.certificateProduct.service;

import net.dgg.framework.tac.utils.idutil.DggKeyWorker;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import net.dgg.zz.common.base.service.BaseService;
import net.dgg.zz.common.utils.ValidateUtils;
import net.dgg.zz.credential.certificateProduct.dao.CertificateProductDao;
import net.dgg.zz.credential.certificateProduct.entity.CertificateProduct;
import net.dgg.zz.production.entity.ProductOrder;
import net.dgg.zz.production.excption.ProductOrderExcption;
import net.dgg.zz.production.inProcess.dao.ProductOrderDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>@Title 证书/资质 和 生产中间表Service</p>
 * <p>@Description 办理中Service</p>
 * <p>@Version 1.0.0 版本号</p>
 * <p>@author maoquanyi</p>
 * <p>@date 2018年08月28日</p>
 * <p>Copyright © dgg group.All Rights Reserved. 版权信息</p>
 */
@Service
public class CertificateProductService extends BaseService {

    @Autowired
    private SessionManager sessionManager;
    @Resource
    private CertificateProductDao certificateProductDao;
    @Resource
    private ProductOrderDao productOrderDao;

    /**
     * <p>@Description 保存证书/资质  生产中间表</p>
     * <p>@author mqy</p>
     * <p>@Time 2018/8/28 0028 10:05 创建时间</p>
     *
     * @param certificateId 证书id
     * @param scOrderId     生产订单ID
     *                      注意:  操作记录实体,在外面自己保存
     *                      <p>@return 中间表的ID</p>
     */
    public Long saveCertifateProduct(Long certificateId, Long scOrderId) {
        ValidateUtils.strNotEmpty(certificateId, ProductOrderExcption.class, "证书/资质ID为空,请刷新重试！");
        UserEntity userEntity = sessionManager.getCurrentUser();
        ValidateUtils.strNotEmpty(userEntity, ProductOrderExcption.class, "未获取到登陆人，请重新登录！");
        ProductOrder productOrder = productOrderDao.selectByPrimaryKey(scOrderId);
        ValidateUtils.strNotEmpty(productOrder, ProductOrderExcption.class, "未查询到对应的生产订单,请刷新重试！");
        CertificateProduct certificateProduct = new CertificateProduct();
        certificateProduct.setId(DggKeyWorker.nextId());
        certificateProduct.setBaseId(certificateId);
        certificateProduct.setScOrderId(productOrder.getId());
        certificateProduct.setScOrderNo(productOrder.getScProductOrderNo());
        certificateProduct.setOrderNo(productOrder.getOrderNo());//订单编号
        certificateProduct.setSubOrderNo(productOrder.getOrderBusNo());//子订单编号
        this.saveOpt(certificateProduct, userEntity);
        certificateProductDao.insert(certificateProduct);
        return certificateProduct.getId();
    }

    /**
     * <p>@Description 保存证书/资质  生产中间表</p>
     * <p>@author mqy</p>
     * <p>@Time 2018/8/28 0028 10:05 创建时间</p>
     *
     * @param certificateId    证书id
     * @param isCertifateOrder 是否是证书订单 0-不是 1-是  默认0
     * @param scOrderId        生产订单ID
     *                         注意:  操作记录实体,在外面自己保存
     *                         <p>@return 中间表的ID</p>
     */
    public CertificateProduct saveCertifateProduct(Long certificateId, Long scOrderId, Integer isCertifateOrder) {
        ValidateUtils.strNotEmpty(certificateId, ProductOrderExcption.class, "证书/资质ID为空,请刷新重试！");
        UserEntity userEntity = sessionManager.getCurrentUser();
        ValidateUtils.strNotEmpty(userEntity, ProductOrderExcption.class, "未获取到登陆人，请重新登录！");
        ProductOrder productOrder = productOrderDao.selectByPrimaryKey(scOrderId);
        ValidateUtils.strNotEmpty(productOrder, ProductOrderExcption.class, "未查询到对应的生产订单,请刷新重试！");
        CertificateProduct certificateProduct = new CertificateProduct();
        certificateProduct.setId(DggKeyWorker.nextId());
        certificateProduct.setBaseId(certificateId);
        certificateProduct.setScOrderId(productOrder.getId());
        certificateProduct.setScOrderNo(productOrder.getScProductOrderNo());
        certificateProduct.setOrderNo(productOrder.getOrderNo());//订单编号
        certificateProduct.setSubOrderNo(productOrder.getOrderBusNo());//子订单编号
        certificateProduct.setIsCertifateOrder(isCertifateOrder);//子订单编号
        this.saveOpt(certificateProduct, userEntity);
        certificateProductDao.insert(certificateProduct);
        return certificateProduct;
    }


    /**
     * 根据证书id查询证书绑定的销售子订单信息
     *
     * @param id
     * @return
     */
    public String findOrderIdsByCredentialId(Long id) {
        return certificateProductDao.findOrderIdsByCredentialId(id);
    }

    /**
     * 根据销售订单编号查询该销售订单绑定的证书绑定的最新的销售订单No
     * @param orderBusinessNo
     * @return
     */
    public Map findOrderInfoByOrderNo(String orderBusinessNo) {
        return certificateProductDao.findOrderInfoByOrderNo(orderBusinessNo);
    }
}
